package com.block.api.controller.expose;

import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.block.channel.service.IChannelActionMonitorRecordService;
import com.block.common.exception.NotifyExceptionService;
import com.block.common.service.LogFactory;
import com.block.merchant.service.IMerchantBaseInfoService;
import com.block.merchant.service.infotype.MerchantApiBHandleService;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Map;

@RestController
@RequestMapping("apinotice/channel")
public class NoticeChannelController {

    private static final Logger log = LogFactory.getNoticeMerLog();

    @Autowired
    NotifyExceptionService notifyExceptionService;
    @Autowired
    IChannelActionMonitorRecordService channelActionMonitorRecordService;

    /**
     *  https://api.longzankeji.cn/apinotice/channel/notifyResult/baidu/baidu001
     *  https://api.longzankeji.cn/apinotice/channel/notifyResult/baidu/baidu002
     *  https://api.longzankeji.cn/apinotice/channel/notifyResult/baidu/baidu003
     *  https://api.longzankeji.cn/apinotice/channel/notifyResult/baidu/baidu004
     *
     *  https://api.longzankeji.cn/apinotice/channel/notifyResult/baidu/baidu001lqb
     *
     *  https://api.longzankeji.cn/apinotice/channel/notifyResult/baidu/baidu001lqb?bd_vid=__BD_VID__&imei_md5=__IMEI_MD5__&oaid=__OAID__&imei=__IMEI__&ip=__IP__&ts=__TS__&userid=__USER_ID__&pid=__PLAN_ID__&uid=__USER_ID__&aid=__IDEA_ID__&click_id=__CLICK_ID__&ext_info={{EXT_INFO}}&callback_url=__CALLBACK_URL__&callType=v2
     *  https://api.longzankeji.cn/apinotice/channel/notifyResult/baidu/baidu001kejie?bd_vid=__BD_VID__&imei_md5=__IMEI_MD5__&oaid=__OAID__&imei=__IMEI__&ip=__IP__&ts=__TS__&userid=__USER_ID__&pid=__PLAN_ID__&uid=__USER_ID__&aid=__IDEA_ID__&click_id=__CLICK_ID__&ext_info={{EXT_INFO}}&callback_url=__CALLBACK_URL__&callType=v2
     */

    /**
     * 授权结果通知
     * apinotice/channel/notifyResult/{}/{}
     * @param request
     * @return
     */
    @RequestMapping(value = "/notifyResult/{platform}/{channelCode}", method = {RequestMethod.POST, RequestMethod.GET})
    public String notifyResult(HttpServletRequest request,
                               @PathVariable("platform") String platform,
                               @PathVariable("channelCode") String channelCode,
                               @RequestParam(value = "actionType", required = false) String actionType) {

        Map<String, String[]> parameterMap = null;
        String paramContent = null;
        try {
            parameterMap = request.getParameterMap();
            paramContent = IoUtil.read(request.getInputStream(), Charset.defaultCharset());
            log.info("【投放监测】【platform】{}【channelCode】{}【actionType】{}【parameterMap】{}【paramContent】{}",
                    platform, channelCode, actionType, JSON.toJSONString(parameterMap), paramContent);

            channelActionMonitorRecordService.saveMonitorInfo(platform, channelCode, actionType, parameterMap, paramContent);

            return "success";
        } catch (Exception exception) {
            String content = StrUtil.format("【channelCode】{}【parameterMap】{}【paramContent】{}",
                    channelCode, JSON.toJSONString(parameterMap), paramContent);
            notifyExceptionService.notifyNoticeInfo("MERCHANT_NOTICE_RESULT", "商户授权结果通知", content, exception);
        }
        return "exception";
    }

    /**
     * apinotice/channel/callback/vivo/vivo/authcode
     * @param request
     * @param platform
     * @param channelCode
     * @param function
     * @return
     */
    @RequestMapping(value = "/callback/{platform}/{channelCode}/{function}", method = {RequestMethod.POST, RequestMethod.GET})
    public String callbackCommon(HttpServletRequest request,
                               @PathVariable("platform") String platform,
                               @PathVariable("channelCode") String channelCode,
                               @PathVariable("function") String function) {

        Map<String, String[]> parameterMap = null;
        String paramContent = null;
        try {
            parameterMap = request.getParameterMap();
            paramContent = IoUtil.read(request.getInputStream(), Charset.defaultCharset());
            log.info("【渠道回调】【platform】{}【channelCode】{}【parameterMap】{}【paramContent】{}",
                    platform, channelCode,  JSON.toJSONString(parameterMap), paramContent);

            channelActionMonitorRecordService.handleCallbackInfo(platform, channelCode, function, parameterMap, paramContent);

            return "success";
        } catch (Exception exception) {
            String content = StrUtil.format("【channelCode】{}【parameterMap】{}【paramContent】{}",
                    channelCode, JSON.toJSONString(parameterMap), paramContent);
            notifyExceptionService.notifyNoticeInfo("MERCHANT_NOTICE_RESULT", "渠道回调结果通知", content, exception);
        }
        return "exception";
    }

}
